package br.com.actionsystem.gdp.util;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JOptionPane;

public class LayoutHelp {

    static GregorianCalendar gc;

    public static GridBagConstraints addLabel() {
        GridBagConstraints cons = new GridBagConstraints();
        cons.fill = GridBagConstraints.BOTH;
        cons.anchor = GridBagConstraints.NORTHWEST;
        cons.insets = new Insets(4, 7, 4, 4);
        cons.weightx = 0;
        cons.gridwidth = 1;

        return cons;
    }

    public static GridBagConstraints addComponentFinal() {
        GridBagConstraints cons = new GridBagConstraints();
        cons.fill = GridBagConstraints.BOTH;
        cons.anchor = GridBagConstraints.NORTHWEST;
        cons.insets = new Insets(4, 4, 4, 4);
        cons.weightx = 0;
        cons.gridwidth = GridBagConstraints.REMAINDER;
        return cons;
    }

    public static GridBagConstraints addComponent1() {
        GridBagConstraints cons = new GridBagConstraints();
        cons.fill = GridBagConstraints.BOTH;
        cons.anchor = GridBagConstraints.NORTHWEST;
        cons.insets = new Insets(4, 4, 4, 4);
        cons.weightx = 0;
        cons.gridwidth = 1;
        return cons;
    }

    public static GridBagConstraints addComponent2() {
        GridBagConstraints cons = new GridBagConstraints();
        cons.fill = GridBagConstraints.BOTH;
        cons.anchor = GridBagConstraints.NORTHWEST;
        cons.insets = new Insets(4, 4, 4, 4);
        cons.weightx = 0;
        cons.gridwidth = 2;
        return cons;
    }

// auhsaush tava tdo componet
    public static GridBagConstraints addComponent3() {
        GridBagConstraints cons = new GridBagConstraints();
        cons.fill = GridBagConstraints.BOTH;
        cons.anchor = GridBagConstraints.NORTHWEST;
        cons.insets = new Insets(4, 4, 4, 4);
        cons.weightx = 0;
        cons.gridwidth = 3;
        return cons;
    }

    public static GridBagConstraints addComponent4() {
        GridBagConstraints cons = new GridBagConstraints();
        cons.fill = GridBagConstraints.BOTH;
        cons.anchor = GridBagConstraints.NORTHWEST;
        cons.insets = new Insets(4, 4, 4, 4);
        cons.weightx = 0;
        cons.gridwidth = 4;
        return cons;
    }

    public static String getActualDate() {
        DecimalFormat df = new DecimalFormat("00");
        gc = new GregorianCalendar();
        String d = df.format(gc.get(Calendar.DAY_OF_MONTH));
        String m = df.format(gc.get(Calendar.MONTH) + 1);
        String y = String.valueOf(gc.get(Calendar.YEAR));
        return String.valueOf(d + "/" + m + "/" + y);
    }

    public static int getMax(String sql) {

        Connection con = null;
        ResultSet rs = null;
        Statement stmt = null;
        int i = 0;
        try {
            con = Connect.getConection();
            con.setAutoCommit(true);
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);
            rs.next();
            i = rs.getInt(1);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Não foi possivel pegar o ultimo registro");
            e.printStackTrace();
            return i;
        } finally {
            try {
                stmt.close();
                rs.close();
                con.close();
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(null, "Não foi possivel conectar ao banco de dados");
                e.printStackTrace();
            }
        }
        return i;

    }

    public static String dateToStr(String text) {
        String[] aFinal = text.split("-");
        String t = aFinal[2] + aFinal[1] + aFinal[0];

        return t;
    }
}
